package com.comtime.mybox;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MySharedPreferences;
import com.comtime.view.SelectNameModePopupWindow;
import com.comyou.customviews.SlideListView;

public class FingerprintManagerActivity extends BaseActivity {

	SlideListView slideListView;
	TextView tv_times;
	String[] values;
	MySharedPreferences mySharedPreferences;
	ToggleButton tb_wrong_pwd;//输入错误密码音乐开关


	HBApplication application;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fingerprint);
		application=(HBApplication) getApplication();
		mySharedPreferences = MySharedPreferences.getInstance(this);
		init();
		
		
	}

	void init() {
		values = new String[] { "10", "30", "60" };
		bufflostWainNum= mySharedPreferences.getDeviceLockTimes();

		 
		tv_times=(TextView) findViewById(R.id.tv_time);
		tv_times.setText(values[bufflostWainNum]);
		tb_wrong_pwd=(ToggleButton) findViewById(R.id.tb_wrong_pwd);
		tb_wrong_pwd.setChecked(mySharedPreferences.getWrongPwdWain());
		tb_wrong_pwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean b) {
				mySharedPreferences.saveWrongPwdWain(b);
				
			}
		});
		
		slideListView = (SlideListView) findViewById(R.id.listview);
		slideListView.setAdapter(adapter);// 绑定adapter
		slideListView.setRightBtnWidth(dp2px(85));// 设置侧滑删除Butten宽度
		adapter.notifyDataSetChanged();
	}

	public void BackAction(View v) {
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}

	/**
	 * add new
	 * 
	 * @param v
	 */
	public void AddFringerAction(View v) {
		Intent intent=new Intent(this, AddFringerActivity.class);
		startActivity(intent);
	}

	public void LockTimeAction(View v) {
		showSelectNameModePopupWindow(bufflostWainNum, values,
				getText(R.string.box_lock_time).toString());
	}

	BaseAdapter adapter = new BaseAdapter() {

		@Override
		public View getView(int position, View view, ViewGroup group) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view = LayoutInflater.from(FingerprintManagerActivity.this)
						.inflate(R.layout.list_item_fringerprint, null);
				holder.tv_name = (TextView) view.findViewById(R.id.tv_name);

				holder.btn_delete = (Button) view.findViewById(R.id.btn_delete);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

			return view;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		@Override
		public void notifyDataSetChanged() {
			// TODO Auto-generated method stub
			super.notifyDataSetChanged();

			// 设置gridview高度
			float density = getResources().getDisplayMetrics().density;
			LayoutParams params_list = (LayoutParams) slideListView
					.getLayoutParams();
			params_list.height = (int) (getCount() * 45 * density);
			slideListView.setLayoutParams(params_list);
		}

	};

	private class ViewHolder {
		TextView tv_name;
		Button btn_delete;
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	SelectNameModePopupWindow selectNameModePopupWindow;

	void showSelectNameModePopupWindow(int currentrvalue, String[] values,
			String title) {
		selectNameModePopupWindow = new SelectNameModePopupWindow(
				FingerprintManagerActivity.this, itemsOnClick,
				valueChangeListener, currentrvalue, values, title);
		// 显示窗口
		selectNameModePopupWindow.showAtLocation(
				findViewById(R.id.activity_finger),
				Gravity.BOTTOM, 0, 0); // 设置layout在PopupWindow中显示的位置
	}

	OnClickListener itemsOnClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_setting_selectalarmtimes_ok:
				tv_times.setText(values[bufflostWainNum]);
				
				 
				mySharedPreferences
						.saveDeviceLockTimes(bufflostWainNum);

				selectNameModePopupWindow.dismiss();
				
				application.swDevicesList.get(0).sendLockTimeMode(bufflostWainNum);
				
				break;
			case R.id.btn_setting_selectalarmtimes_cancel:
				selectNameModePopupWindow.dismiss();
				break;

			default:
				break;
			}
		}
	};

	int bufflostWainNum = 1;
	OnValueChangeListener valueChangeListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			// TODO Auto-generated method stub
			if (picker.getId() == R.id.numberPicker_setting_times) {
				bufflostWainNum = newVal;

			}
		}
	};

	
	
	
	
}
